
*=============================================================================*
* Figure 1 and A.5
*=============================================================================*
gen hh_GI = hh_grossinc
gen hh_age = age


foreach X in "MNW" "GI" "age" {
	global X = "`X'"
	capture drop hh_${X}_sortby
	gen hh_${X}_sortby = hh_${X} if year==2018 & hh_head_2012==1 & age>=18 & !missing(hhid_2012) & hhid_2012>0

	sort hh_${X}_sortby

	capture drop hh_${X}_qtile_2018*
	sum hh_${X}_sortby
	gen long hh_${X}_qtile_2018 = int(1000*(_n-1)/`r(N)') if !missing(hh_${X}_sortby)

	replace hh_${X}_qtile_2018 = 990 if inrange(hh_${X}_qtile_2018,990,1000)
	replace hh_${X}_qtile_2018 = 950 if inrange(hh_${X}_qtile_2018,950,989)
	replace hh_${X}_qtile_2018 = 900 if inrange(hh_${X}_qtile_2018,900,949)
	replace hh_${X}_qtile_2018 = floor(hh_${X}_qtile_2018/100)*100 if hh_${X}_qtile_2018<900

	sort hh_${X}_qtile_2018
	capture drop hh_gaver_*_qmean_${X}
	capture drop hh_gaver_*_qpart_${X}
	by hh_${X}_qtile_2018: gen hh_${X}_qtile_2018_n=_n
	foreach var in "gaver_tot"  "gaver_aid" "gaver_rel" "gaver_nonrel" "gaver_medical" "gaver_social" "gaver_climate" "gaver_humrig" "gaver_domserv" "gaver_rest" {
		disp "${S_TIME} .... `var' ....."
		by hh_${X}_qtile_2018: egen hh_`var'_qmean_${X} = mean(hh_`var')
		by hh_${X}_qtile_2018: egen hh_`var'_qpart_${X} = mean(hh_`var'>0)
		if inlist("$X","MNW","GI") {
			capture drop hh_`var'_G2I
			capture drop hh_`var'_G2W
			gen hh_`var'_G2I = min(hh_`var'/hh_GI,10) if !missing(hh_`var'/hh_GI)
			gen hh_`var'_G2W = max(min(hh_`var'/hh_MNW,10),0) if !missing(hh_`var'/hh_MNW)
			capture drop hh_`var'_G2*_${X}
			by hh_${X}_qtile_2018: egen hh_`var'_G2I_${X} = mean(hh_`var'_G2I)
			by hh_${X}_qtile_2018: egen hh_`var'_G2W_${X} = mean(hh_`var'_G2W)
		}
		
	}
	replace hh_${X}_qtile_2018_n = . if missing(hh_${X}_qtile_2018)
	* Stacked giving variables
	capture drop stacked_zero
	capture drop stacked_*_${X}
	gen byte stacked_zero = 0

	* Grouping; smallest shares first (easier to add text desc in figure)
	*	means
	gen stacked_domestic_${X} 	= stacked_zero 		 + hh_gaver_social_qmean_${X} +hh_gaver_domserv_qmean_${X}+hh_gaver_medical_qmean_${X}+ hh_gaver_rest_qmean_${X}
	gen stacked_religious_${X} 	= stacked_domestic_${X}  + hh_gaver_rel_qmean_${X}
	gen stacked_internat_${X}	= stacked_religious_${X} + hh_gaver_humrig_qmean_${X} + hh_gaver_aid_qmean_${X}+ hh_gaver_climate_qmean_${X}
	*	participation
	gen stacked_domestic_part_${X} 	= stacked_zero 		      + hh_gaver_social_qpart_${X} +hh_gaver_domserv_qpart_${X}+hh_gaver_medical_qpart_${X}+ hh_gaver_rest_qpart_${X}
	gen stacked_religious_part_${X} = stacked_domestic_part_${X}  + hh_gaver_rel_qmean_${X}
	gen stacked_internat_part_${X}	= stacked_religious_part_${X} + hh_gaver_humrig_qpart_${X} + hh_gaver_aid_qpart_${X}+ hh_gaver_climate_qpart_${X}

	* xlabels, want to to spread out the top quantiles for readibility
	capture drop _x_${X}
	gen long _x_${X} = hh_${X}_qtile_2018
	replace _x_${X} = 1000 if _x_${X}==990
}

* AWTR across wealth distributions
bysort hh_MNW_qtile_2018: egen hh_qmean_AWTR_MNW = mean(hh_AWTR) if !missing(hh_MNW_qtile_2018)

	
* Age
sum hh_age if hh_age_qtile_2018_n==1 & _x_age==100
global age100 = `r(mean)'
sum hh_age if hh_age_qtile_2018_n==1 & _x_age==500
global age500 = `r(mean)'
sum hh_age if hh_age_qtile_2018_n==1 & _x_age==900
global age900 = `r(mean)'
#delimit;
twoway 	(rarea	stacked_internat_age stacked_zero 	_x_age if hh_age_qtile_2018_n==1, sort lcolor(blue) lwidth(none) fcolor(gs4%10)	)
	(line	stacked_internat_age 			_x_age if hh_age_qtile_2018_n==1, sort lpattern(solid) lwidth(thick) lcolor(blue)	)
	(line	stacked_religious_age 			_x_age if hh_age_qtile_2018_n==1, sort lpattern(longdash) lcolor(blue)	)
	(line	stacked_domestic_age			_x_age if hh_age_qtile_2018_n==1, sort lpattern(dash) lcolor(blue)	)
	, graphregion(color(white))
	xlabel(100 `" "10%" "(Age = ${age100})" "' 200 "20" 300 "30" 400 "40" 500 "50" 600 "60" 700 "70" 800 "80" 900 `" "90%" "(Age = ${age900})" "' 950 "95" 1000 "99%" , labsize(small))
	ylabel(, glcolor(gs14))
	legend(off)
	ytitle("Mean Charitable Giving in 2018 (NOK)")
	text( 120 710  "Domestic" 850 710  "Religious"  1800 710 "International", color(gs4) placement(east))
	xtitle("Percentile of the Age Distribution")
	;
#delimit cr
graph save ${figures}/wdon1_giving_across_distribution_age.gph, replace
graph export ${figures}/wdon1_giving_across_distribution_age.pdf,  as(pdf) replace


#delimit;
twoway 	(rarea	stacked_internat_MNW stacked_zero 	_x_MNW if hh_MNW_qtile_2018_n==1, sort lcolor(blue) lwidth(none) fcolor(gs4%10)	)
	(line	stacked_internat_MNW 			_x_MNW if hh_MNW_qtile_2018_n==1, sort lpattern(solid) lwidth(thick) lcolor(blue)	)
	(line	stacked_religious_MNW 			_x_MNW if hh_MNW_qtile_2018_n==1, sort lpattern(longdash) lcolor(blue)	)
	(line	stacked_domestic_MNW			_x_MNW if hh_MNW_qtile_2018_n==1, sort lpattern(dash) lcolor(blue)	)
	, graphregion(color(white))
	xlabel(100 "10%" 200 "20" 300 "30" 400 "40" 500 "50" 600 "60" 700 "70" 800 "80" 900 "90" 950 "95" 1000 "99%" , labsize(small))
	ylabel(, glcolor(gs14))
	legend(off)
	ytitle("Mean Charitable Giving in 2018 (NOK)")
	text( 120 710  "Domestic" 850 710  "Religious"  1800 710 "International", color(gs4) placement(east))
	xtitle("Percentile of the Net Wealth Distribution")
	;
#delimit cr
graph save ${figures}/wdon1_giving_across_distribution_wealth.gph, replace
graph export ${figures}/wdon1_giving_across_distribution_wealth.pdf,  as(pdf) replace




#delimit;
twoway 	(rarea	stacked_internat_GI stacked_zero _x_GI if hh_GI_qtile_2018_n==1,  sort lcolor(blue) lwidth(none) fcolor(gs4%10)	)
	(line	stacked_internat_GI 		 _x_GI if hh_GI_qtile_2018_n==1,  sort lpattern(solid) lwidth(thick) lcolor(blue)	)
	(line	stacked_religious_GI 		 _x_GI if hh_GI_qtile_2018_n==1,  sort lpattern(longdash) lcolor(blue)	)
	(line	stacked_domestic_GI		 _x_GI if hh_GI_qtile_2018_n==1,  sort lpattern(dash) lcolor(blue)	)
	, graphregion(color(white))
	xlabel(100 "10%" 200 "20" 300 "30" 400 "40" 500 "50" 600 "60" 700 "70" 800 "80" 900 "90" 950 "95" 1000 "99%" , labsize(small))
	ylabel(, glcolor(gs14))
	legend(off)
	ytitle("Mean Charitable Giving in 2018 (NOK)")
	text( 110 710  "Domestic" 850 710  "Religious"  1800 710 "International", color(gs4) placement(east))
	xtitle("Percentile of the Income Distribution")
	;
#delimit cr
graph save ${figures}/wdon1_giving_across_distribution_income.gph, replace
graph export ${figures}/wdon1_giving_across_distribution_income.pdf,  as(pdf) replace


* Participation across all the distributions 
set scheme s2mono, permanently
#delimit;
twoway 	
	(line	hh_gaver_tot_qpart_age			_x_age 	if hh_age_qtile_2018_n==1, sort lpattern(dash) lcolor(blue%90) 	  lwidth(medthick)) 
	(line	hh_gaver_tot_qpart_GI			_x_GI 	if hh_GI_qtile_2018_n==1,  sort lpattern(longdash) lcolor(gray%35) lwidth(medthick) )
	(line	hh_gaver_tot_qpart_MNW			_x_MNW 	if hh_MNW_qtile_2018_n==1, sort lpattern(solid) lcolor(orange)  lwidth(medthick) )
	, graphregion(color(white))
	xlabel(100 "10%" 200 "20" 300 "30" 400 "40" 500 "50" 600 "60" 700 "70" 800 "80" 900 "90" 950 "95" 1000 "99%", labsize(small) )
	ylabel(, glcolor(gs14))
	ytitle("Probability of Charitable Giving")
	xtitle("Percentile of Wealth, Income, or Age Distribution")
	legend(	subtitle("Participation") order(1 "Sorted by age" 2 "by income" 3 "by wealth" ) rows(3) cols(1) ring(0) pos(11) margin(0 3 0 0) bmargin(5 0 0 3) symxsize(*0.75) region(lcolor(white%0) color(white%0)) )
	;
#delimit cr
graph save ${figures}/wdon1_giving_across_distribution_participation_alldistributions.gph, replace
graph export ${figures}/wdon1_giving_across_distribution_participation_alldistributions.pdf,  as(pdf) replace


*[FIGURE 1] Combined figure
#delimit;
gr combine 	${figures}/wdon1_giving_across_distribution_age.gph
		${figures}/wdon1_giving_across_distribution_wealth.gph 
		${figures}/wdon1_giving_across_distribution_income.gph
		${figures}/wdon1_giving_across_distribution_participation_alldistributions.gph
		, xsize(1.5) ysize(1) cols(2) rows(2) graphregion(color(white) margin(tiny tiny tiny tiny)) imargin(1 1 5 1) iscale(0.6);
#delimit cr
graph export ${figures}/wdon1_giving_across_distribution_combined_2x2.pdf,  as(pdf) replace



* [FIGURE A.5] Giving as a Share of Income and Wealth and AWTR across wealth distribution
#delimit;
twoway 	
	(line	hh_gaver_tot_G2I_MNW		 _x_MNW if hh_MNW_qtile_2018_n==1 & _x_MNW>400 ,  sort lpattern(dash) lcolor(gray%75)	)
	(line	hh_gaver_tot_G2W_MNW		 _x_MNW if hh_MNW_qtile_2018_n==1 & _x_MNW>400,  sort lpattern(solid) lcolor(blue) lwidth(thick)	)
	(line	hh_qmean_AWTR_MNW		 _x_MNW if hh_MNW_qtile_2018_n==1 & _x_MNW>400,  sort lpattern(shortdash) lcolor(green%80) lwidth(thick)	)
	
	, graphregion(color(white))
	ylabel(0.0001 "0.01%" 0.001 "0.1%" 0.002 "0.2" 0.003 "0.3" 0.004 "0.4" 0.005 "0.5%" , labsize(small))
	ylabel(, glcolor(gs14))
	xlabel(/*100 "10%" 200 "20" 300 "30" 400 "40" */ 500 "50%" 600 "60" 700 "70" 800 "80" 900 "90" 950 "95" 1000 "99%" , labsize(small))
	
	legend(	order(2 "Giving/Net Wealth" 3 "Wealth Tax/Net Wealth" 1 "Giving/Gross Income" ) rows(3) cols(1) ring(0) pos(9) margin(0 3 0 0) bmargin(2 0 5 0) symxsize(*0.75) region(lcolor(white%0) color(white%0)) )

	ytitle("Share of Income or Wealth (in %)")
	xtitle("Percentile of Net Wealth Distribution")
	xsize(1.75) ysize(1)
	;
#delimit cr
graph save ${figures}/wdon1_giving_as_share_of_wealth.gph,  replace

graph export ${figures}/wdon1_giving_as_share_of_wealth.pdf,  as(pdf) replace



* some statistics for mentioning in the text
sum hh_MNW if year==2018 & hh_head_2012==1 & age>=18 & !missing(hhid_2012) & hhid_2012>0,d




